using System;

namespace OpenWaves.ImageTransformations.Web
{
    public class HmacImageTransformationUrlValidationRule : IImageTransformationUrlValidationRule
    {
        public bool ValidateImageTransformationUrl(string imageTransformation, Url url)
        {
            string hash;
            if (url.Query.TryGetValue("ts", out hash) == false)
                return false;

            if (String.IsNullOrEmpty(hash))
                return false;

            if (hash.Equals(MachineKey.HashData(url.Path + imageTransformation), StringComparison.Ordinal) == false)
                return false;

            return true;
            
        }

        public void AugmentImageTransformationUrl(string imageTransformation, UrlBuilder urlBuilder)
        {
            urlBuilder.AddParameter("ts", MachineKey.HashData(urlBuilder.Path + imageTransformation));            
        }
    }
}